package gogogo.liwushuo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.CgLeftBottomBean;
import gogogo.liwushuo.util.CommonAdapter;
import gogogo.liwushuo.util.MyHolder;
import gogogo.liwushuo.util.OKHttpUtils;

public class CgLeftCheckAll_2 extends AppCompatActivity {

    @InjectView(R.id.rv_cgleft_checkall_2)
    RecyclerView mRecyelViewCheckall2;
    @InjectView(R.id.iv_cgleft_checkall_2_title)
    TextView mIvCgleftCheckall2Title;
    @InjectView(R.id.swip_refresh_cgleft_checkall_2)
    SwipeRefreshLayout swipRefreshCgleftCheckall2;
    private String mUrl;
    private int mPosition;
    private CommonAdapter<CgLeftBottomBean.ChannelBean> mAdapter;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cg_left_check_all_2);
        ButterKnife.inject(this);

        Bundle bundle = getIntent().getExtras();
        mUrl = bundle.getString("url");
        mPosition = bundle.getInt("position");

        loadDatas();
        mHandler = new Handler();
        swipRefreshCgleftCheckall2.setColorSchemeColors(Color.BLUE);
        swipRefreshCgleftCheckall2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadDatas();
                        swipRefreshCgleftCheckall2.setRefreshing(false);
                    }
                },1500);
            }
        });
    }

    private void loadDatas() {
        OKHttpUtils.newInstance(this).getAsyncMainData(mUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                CgLeftBottomBean bottomBean = new Gson().fromJson(result, CgLeftBottomBean.class);
                mAdapter = new CommonAdapter<CgLeftBottomBean.ChannelBean>
                        (CgLeftCheckAll_2.this, bottomBean.data.channel_groups.get(mPosition).channels, R.layout.item_cgleft_checkall_2) {
                    @Override
                    public void bindData(MyHolder holder, int position, CgLeftBottomBean.ChannelBean bean) {
                        holder.displayImage(bean.cover_image_url, R.id.iv_cgleft_checkall_2);
                    }
                };
                mRecyelViewCheckall2.setAdapter(mAdapter);
                mRecyelViewCheckall2.setLayoutManager(new GridLayoutManager(CgLeftCheckAll_2.this, 2));

                mIvCgleftCheckall2Title.setText(bottomBean.data.channel_groups.get(mPosition).name);

                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.iv_back_checkall_2)
    public void onClick() {
        finish();
    }
}
